﻿<%@ WebHandler Language="C#" Class="UncheckWorkflowstepReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 未完工序查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2025.4.22
 **/

public class UncheckWorkflowstepReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSql(JObject jObject)
    {
//        string sql = @"
//SELECT f.factoryname,cci.mfgordername,mfg.plannedstartdate,mfg.plannedcompletiondate,e.fullname,cci.containername,cci.productdescription,cci.qty,cci.productname,
//       cci.productrevision,cci.workflowname,cci.workflowrevision,csd.specno,ws.workflowstepname,ws.makeempcounts,w.workcentername,te.teamname,
//       ws.makecycle,ws.preparecycle,ws.transfercycle,ws.shipcycle,ws.commicycle,ws.chargingqty,
//       listagg(rd.resourcename,'、') WITHIN GROUP (ORDER BY rd.resourcename) resourcelist,
//       NULL workflow_createdate,NULL step_type
//FROM (
//  SELECT csd.*,row_number () OVER (PARTITION BY csd.containerid, csd.workflowstepid ORDER BY csd.checkdate) as rn
//  FROM containersteprecord csd
//  WHERE csd.usecompleted = 0
//) csd
//INNER JOIN containercurrentinfo cci ON cci.containerid = csd.containerid
//LEFT JOIN workflowstep ws ON ws.workflowstepid = csd.workflowstepid
//LEFT JOIN specresourceinfo sr ON sr.specid = csd.specid
//LEFT JOIN resourcedef rd ON rd.resourceid = sr.resourceid
//LEFT JOIN mfgorder mfg ON mfg.mfgorderid = cci.mfgorderid
//INNER JOIN container c ON c.containerid = cci.containerid
//LEFT JOIN employee e ON e.employeeid = c.startemployeeid
//LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
//LEFT JOIN workcenter w ON w.workcenterid = ws.workcenterid
//LEFT JOIN team te ON te.teamid = ws.teamid
//LEFT JOIN containerstepnoship cns ON cns.containerid = csd.containerid AND cns.workflowstepid = csd.workflowstepid AND cns.noship = 2
//WHERE csd.rn = 1 AND cci.containerstatus = 1 AND cns.containerstepnoshipid IS NULL";
        string sql = @"
SELECT f.factoryname,cci.mfgordername,mfg.plannedstartdate,mfg.plannedcompletiondate,e.fullname,cci.containername,cci.productdescription,cci.qty,cci.productname,
       cci.productrevision,cci.workflowname,cci.workflowrevision,s.specno,s.opdesc workflowstepname,ws.makeempcounts,w.workcentername,te.teamname,
       ws.preparecycle,ws.makecycle,ws.transfercycle,ws.shipcycle,ws.commicycle,listagg(rd.resourcename,'/') WITHIN GROUP (ORDER BY rd.resourcename) resourcelist,
       ws.chargingqty,NULL workflow_createdate,NULL step_type
FROM containercurrentinfo cci
INNER JOIN container c ON c.containerid = cci.containerid
LEFT JOIN workflowstep ws ON ws.workflowid = cci.workflowid
LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
LEFT JOIN spec s ON s.specid = NVL(sb.revofrcdid,ws.specid)
LEFT JOIN containerstepnoship cns ON cns.containerid = cci.containerid AND cns.workflowstepid = ws.workflowstepid AND cns.noship = 2
LEFT JOIN containersteprecord csd ON csd.containerid = cci.containerid AND csd.workflowstepid = ws.workflowstepid AND csd.usecompleted = 1
LEFT JOIN specresourceinfo sr ON sr.specid = s.specid --工序关联的设备
LEFT JOIN resourcedef rd ON rd.resourceid = sr.resourceid
LEFT JOIN mfgorder mfg ON mfg.mfgorderid = cci.mfgorderid
LEFT JOIN employee e ON e.employeeid = c.startemployeeid
LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
LEFT JOIN workcenter w ON w.workcenterid = ws.workcenterid
LEFT JOIN team te ON te.teamid = ws.teamid
WHERE cci.isinstore = 0 AND cci.qty > 0 AND cci.containerstatus = 1 AND cns.containerstepnoshipid IS NULL AND csd.containersteprecordid IS NULL";
        //主制分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND ws.workcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        //订单计划完成时间
        if (jObject["plannedcompletiondate"] != null && jObject["plannedcompletiondate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["plannedcompletiondate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND mfg.plannedcompletiondate >= to_date('{0} 00:00:00','yyyy-MM-dd hh24:mi:ss') ", arr[0].ToString());
                sql += string.Format(" AND mfg.plannedcompletiondate <= to_date('{0} 23:59:59','yyyy-MM-dd hh24:mi:ss') ", arr[1].ToString());
            }
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        //订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        //计划员
        if (jObject["jihuayuan"] != null && jObject["jihuayuan"].ToString() != "")
        {
            sql += string.Format(" AND c.startemployeeid = '{0}'", jObject["jihuayuan"].ToString());
        }

        sql += @"
GROUP BY f.factoryname,cci.mfgordername,mfg.plannedstartdate,mfg.plannedcompletiondate,e.fullname,cci.containername,cci.productdescription,cci.qty,cci.productname,
         cci.productrevision,cci.workflowname,cci.workflowrevision,s.specno,s.opdesc,ws.makeempcounts,w.workcentername,te.teamname,
         ws.makecycle,ws.preparecycle,ws.transfercycle,ws.shipcycle,ws.commicycle,ws.chargingqty
ORDER BY cci.containername,s.specno";

        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getSql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dt.Columns["factoryname"].Caption = "主制分厂";
        dt.Columns["mfgordername"].Caption = "订单号";
        dt.Columns["plannedstartdate"].Caption = "订单计划开始时间";
        dt.Columns["plannedcompletiondate"].Caption = "订单计划完成时间";
        dt.Columns["fullname"].Caption = "计划员";
        dt.Columns["containername"].Caption = "流水卡号";
        dt.Columns["qty"].Caption = "计划数量";
        dt.Columns["productname"].Caption = "零件图号";
        dt.Columns["productdescription"].Caption = "物料描述";
        dt.Columns["productrevision"].Caption = "零件版本";
        dt.Columns["workflowname"].Caption = "工艺路线";
        dt.Columns["workflowrevision"].Caption = "工艺版本";
        dt.Columns["specno"].Caption = "工序号";
        dt.Columns["workflowstepname"].Caption = "工序名称";
        dt.Columns["makeempcounts"].Caption = "作业人数";
        dt.Columns["workcentername"].Caption = "工区";
        dt.Columns["teamname"].Caption = "班组";
        dt.Columns["makecycle"].Caption = "加工工时";
        dt.Columns["preparecycle"].Caption = "准备工时";
        dt.Columns["transfercycle"].Caption = "转运工时";
        dt.Columns["resourcelist"].Caption = "设备";
        dt.Columns["shipcycle"].Caption = "转工周期";
        dt.Columns["commicycle"].Caption = "外委周期";
        dt.Columns["chargingqty"].Caption = "装炉/槽量";
        dt.Columns["workflow_createdate"].Caption = "工艺创建时间";
        dt.Columns["step_type"].Caption = "工序类型";
        ExportHelper.ExportExcel(dt, "未完工序查询报表");
    }
}